//
//  TestBannerViewController.m
//  MyDemos
//
//  Created by iMac on 2018/3/9.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "TestBannerViewController.h"
#import "WDBannerCollectionView.h"


@interface TestBannerViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WDBannerCollectionView *banner;
@property (nonatomic, strong) NSArray *bannerImages;
@end

@implementation TestBannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self setGroundViewframe:self.tableView hasNavBar:NO hasTabbar:NO];
    [self.view addSubview:self.tableView];
    
    self.banner = [WDBannerCollectionView bannerViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    [self.banner setRepeatTimeInterval:1];
//    NSArray *images = @[[UIImage imageNamed:@"banner01.jpg"]];
    NSArray *images = @[[UIImage imageNamed:@"banner01.jpg"],
                        [UIImage imageNamed:@"banner02.jpg"],
                        [UIImage imageNamed:@"banner03.jpg"],
                        [UIImage imageNamed:@"banner04.jpg"],
                        [UIImage imageNamed:@"banner05.jpg"]];
    
    // CGRectMake(200 - 40, SCREEN_WIDTH - 100, 80, 20)
    self.banner.selectedIndex = ^(NSInteger index) {
//        NSLog(@"selectedIndex: %ld", index);
    };
    self.banner.currentIndex = ^(NSInteger index) {
//        NSLog(@"currentIndex: %ld", index);
    };
    [self.banner  setBannerImages:images];

//    self.tableView.tableHeaderView = self.banner;
    
    NSLog(@"retain  count = %ld\n",CFGetRetainCount((__bridge  CFTypeRef)(self.banner)));
    
}



- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
    [super dismissViewControllerAnimated:flag completion:completion];
//    [self.banner stopTimer];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSString *cellID = @"TestBannerViewControllerCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"-- 这是第   %ld   个cell", indexPath.row];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    [self.banner restart];
    NSLog(@"retain  count = %ld\n",CFGetRetainCount((__bridge  CFTypeRef)(self.banner)));
    return self.banner;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 200;
    }
    return 0;
}

- (void)dealloc {
    NSLog(@"............ dealoc");
    NSLog(@"retain  count = %ld\n",CFGetRetainCount((__bridge  CFTypeRef)(self.banner)));
    
    
}

@end
